import { Component, OnInit,Input,Output,EventEmitter } from '@angular/core';
import { Router,ActivatedRoute} from '@angular/router';
import { HeroService } from '../../../../services/hero.service';
import { NzMessageService,NzModalService } from 'ng-zorro-antd';
import {
  FormBuilder,
  FormGroup,
  Validators
} from '@angular/forms';

@Component({
  selector: 'app-printerconfig',
  templateUrl: './printerconfig.component.html',
  styleUrls: ['./printerconfig.component.css']
})
export class PrinterconfigComponent implements OnInit {

  @Input() childValue:Array<any>=[];
  @Output() event = new EventEmitter();
  validateForm: FormGroup;
  public brandId:string;
  public brandName:string;
  public typeId:string;
  public typecode:string;
  public name:string;
  public radioValue:string;
  formatterPercent = value => `${value} %`;
  parserPercent = value => value.replace(' %', '');

  submitForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[ i ].markAsDirty();
      this.validateForm.controls[ i ].updateValueAndValidity();
    }
  }

  constructor(private heroService: HeroService,
              private fb: FormBuilder,
              private router: Router,
              private route: ActivatedRoute,
              private message: NzMessageService,
              private modalService: NzModalService) { }

  ngOnInit(): void {
    this.validateForm = this.fb.group({
      kWarning: [ 10, [ Validators.required ] ],
      cWarning: [ 10, [ Validators.required ] ],
      mWarning: [ 10, [ Validators.required ] ],
      yWarning: [ 10, [ Validators.required ] ]
    });
  }

  setConfig() {
    if(this.childValue.length>0){
      if(this.validateForm.status=="VALID"){
        let url=host+"/main/printerSet/save";
        let model='printerIds='+this.childValue+'&kWarning='+this.validateForm.value.kWarning+'&cWarning='+this.validateForm.value.cWarning+'&mWarning='+this.validateForm.value.mWarning+'&yWarning='+this.validateForm.value.yWarning;
        this.heroService.post(url,model).then(result=>{
            if(result.code==0){
              this.message.success('批量预警配置成功');
              this.event.emit({ isVisible: false });
            }else{
              this.modalService.error({
                nzTitle: '提示',
                nzContent: result.msg
              });
            }
        })
      }
    }else{
      const id = this.route.snapshot.paramMap.get('id');
      if(this.validateForm.status=="VALID"){
        let url=host+"/main/printerSet/save";
        let model='printerId='+id+'&kWarning='+this.validateForm.value.kWarning+'&cWarning='+this.validateForm.value.cWarning+'&mWarning='+this.validateForm.value.mWarning+'&yWarning='+this.validateForm.value.yWarning;
        this.heroService.post(url,model).then(result=>{
            if(result.code==0){
              this.message.success('预警配置成功');
              this.router.navigate(['/main/printer']);
            }else{
              this.modalService.error({
                nzTitle: '提示',
                nzContent: result.msg
              });
            }
        })
      }
    }
  }
  cancel(){
    this.event.emit({ isVisible: false });
  }

}

